//
//  shoppingController.swift
//  Swift-TableView-Example
//
//  Created by Bilal ARSLAN on 12/10/14.
//  Copyright (c) 2014 Bilal ARSLAN. All rights reserved.
//

import Foundation
import UIKit
import Alamofire
import SwiftyJSON
import KeychainSwift


class shoppingController: UIViewController, PopViewControllerDelegate {

    var popview:PopViewController?
    
    @IBAction func ExtractGift(sender: AnyObject) {
        //点击抽取礼品
        //先对弹出框进行屏蔽，直接进行跳转
        let keychain = KeychainSwift(keyPrefix: "myTestKey_")
        let urladdress = "http://42.62.18.210:8090/apiradio/ar/draw?userID="+keychain.get("registerUserID")!+"&actiID=1"
        print(urladdress)
        Alamofire.request(.GET, urladdress)
            .responseJSON { response in
                if let result = response.result.value {
                    let json = JSON(result)
                    print(result)
                    if (String(json["obj"]["type"]) == "1") {
                        print("抽中奖品了!!!")
                        //self.productImg.image = UIImage(named: String(json["obj"]["img"]))
                        print(json["obj"]["txt"])
                        print(json["obj"]["txt1"])
                        print(json["obj"]["txt2"])
                        print(json["obj"]["txt3"])
                        
                        let imgurl = "http://testimg.qichexiaozi.com/"+String(json["obj"]["img"])
                        self.willpopviewProduct(imgurl, txt1: String(json["obj"]["txt1"]), txt2: String(json["obj"]["txt2"]), txt3: String(json["obj"]["txt3"]), type:String(json["obj"]["type"]), pid:String(json["obj"]["id"]))
                        
                    }else {
                       /// print(json["obj"]["txt"])
                       // print(json["obj"]["txt1"])
                       // print(json["obj"]["txt2"])
                       // print(json["obj"]["txt3"])
                        
                        let imgurl = "http://testimg.qichexiaozi.com/"+String(json["obj"]["img"])
                        self.willpopview(imgurl, txt1: String(json["obj"]["txt1"]), txt2: String(json["obj"]["txt2"]), txt3: String(json["obj"]["txt3"]), type:String(json["obj"]["type"]))
                        
                    }
                    

                    //print(json["obj"]["img"])
                    //self.ProductImg.image = UIImage(named: "btn.9")
                }
        }
    }
    
    func willpopviewProduct(img:String, txt1:String, txt2:String, txt3:String, type:String, pid:String) { 
        //
        let sb = UIStoryboard(name: "Main", bundle: nil)
        popview = (sb.instantiateViewControllerWithIdentifier("popper") as! PopViewController)
        popview?.delegate = self
        popview?.modalTransitionStyle = .CoverVertical
        popview?.modalPresentationStyle = .CurrentContext
        
        popview?.productImageUrl = img
        popview?.productTxt1 = txt1
        popview?.productTxt2 = txt2
        popview?.productTxt3 = txt3
        popview?.productType = type
        popview?.productID   = pid
        
        print("-------******")
        print(popview?.productID)
        
        self.showViewController(popview!, sender: self)
    }
    
    func willpopview(img:String, txt1:String, txt2:String, txt3:String, type:String) {
        //
        let sb = UIStoryboard(name: "Main", bundle: nil)
        popview = (sb.instantiateViewControllerWithIdentifier("popper") as! PopViewController)
        popview?.delegate = self
        popview?.modalTransitionStyle = .CoverVertical
        popview?.modalPresentationStyle = .CurrentContext
        
        popview?.productImageUrl = img
        popview?.productTxt1 = txt1
        popview?.productTxt2 = txt2
        popview?.productTxt3 = txt3
        popview?.productType = type
        
        self.showViewController(popview!, sender: self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarController!.tabBar.hidden = false;
    }
    
    func closePop(sender:AnyObject) {
        self.dismissViewControllerAnimated(true, completion: {})
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}